package com.kust.smarthome.web.imservice;


import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Base64;

@Service
public class Imservice {

    // 客户 ID
    static final String customerKey = "fe4c61c97d2248b9b15f5a4fbbfb60ed";
    // 客户密钥
    static final String customerSecret = "5b0c2dbc4a9947a18303e66e4a4ee7a7";

    // 拼接客户 ID 和客户密钥并使用 base64 编码
    static String plainCredentials = customerKey + ":" + customerSecret;
    static String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
    // 创建 authorization header
    static String authorizationHeader = "Basic " + base64Credentials;

    public static String sendPost(String urlParam) throws HttpException, IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间：15000毫秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        // 创建post请求方法实例对象
        PostMethod postMethod = new PostMethod(urlParam);
        // 设置post请求超时时间
        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);

        postMethod.addRequestHeader("Authorization", "application/json");
        postMethod.addRequestHeader("Content-Type", "application/json");

        httpClient.executeMethod(postMethod);

        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();
        return result;
    }
    public static String sendGet() throws HttpException, IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间：15000毫秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        // 创建GET请求方法实例对象
        GetMethod getMethod = new GetMethod("https://api.agora.io/dev/v1/projects");
        // 设置get请求超时时间
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);

        getMethod.addRequestHeader("Authorization", authorizationHeader);
        getMethod.addRequestHeader("Content-Type", "application/json");

        httpClient.executeMethod(getMethod);

        String result = getMethod.getResponseBodyAsString();
        getMethod.releaseConnection();
        return result;
    }

    public static String sendmessage(String uid,String message) throws IOException {
        // 创建httpClient实例对象
        HttpClient httpClient = new HttpClient();
        // 设置httpClient连接主机服务器超时时间：15000毫秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
        // 创建post请求方法实例对象
        PostMethod postMethod = new PostMethod("  https://api.agora.io/dev/v2/project/2f07c1faa3f94088bd7a3f74c1203292/rtm/users/xiangrikuiserver/peer_messages?wait_for_ack=true");
        // 设置post请求超时时间
        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);

        postMethod.addRequestHeader("Authorization", authorizationHeader);
        postMethod.addRequestHeader("Content-Type", "application/json");

        ObjectNode jsonNode = JsonNodeFactory.instance.objectNode();
        jsonNode.put("destination", uid);
        jsonNode.put("enable_offline_messaging", false);
        jsonNode.put("enable_historical_messaging", false);
        jsonNode.put("payload", message);

        String  toJson = jsonNode.toString();
        RequestEntity se = new StringRequestEntity(toJson ,"application/json" ,"UTF-8");
        postMethod.setRequestEntity(se);
        httpClient.executeMethod(postMethod);

        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();
        return result;
    }
    public static void main(String[] args) throws HttpException, IOException {
        String url ="http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=120.79.75.96";
//        System.out.println(sendPost(url));
        System.out.println(sendmessage("276840251@qq.com","{\"msgtype\":\"CALLVIDEO\"}"));
    }
}